MEGpipeline_HdmLeadfield.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Wrapper for Headmodel & Leadfield generation: %
  3. % Inputs: Segmented MRI & Preprocessed MEG data. %
  4. % Last modified: Jan. 15, 2014 %
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. % Copyright (C) 2013-2014, Michael J. Cheung
  7. %
  8. % This file is a part of the MEG & PLS Pipeline (MEGPLS). For more
  9. % details, see the documentation included with the software package.
  10. %
  11. % MEGPLS is free software: you can redistribute it and/or modify it under
  12. % the terms of the GNU General Public License version 2 as published by
  13. % the Free Software Foundation. This program is distributed in the hope
  14. % that it will be useful, but WITHOUT ANY WARRANTY; without even the
  15. % implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. % See the GNU General Public License for more details.
  17. %
  18. % You should have received a copy of the GNU General Public License along
  19. % with this program. If not, you can download the license here:
  20. % <http://www.gnu.org/licenses/old-licenses/gpl-2.0>.
  21. function MEGpipeline_HdmLeadfield(BuilderMat)
  22. % Make sure java paths added:
  23. UseProgBar = CheckParforJavaPaths;
  24. % Load builder .mat file:
  25. Builder = load(BuilderMat);
  26. name = Builder.name;
  27. paths = Builder.paths;
  28. FTcfg = Builder.FTcfg;
  29. % Clear existing Errorlog & Diary:
  30. RunSection = 'HdmLeadfield';
  31. if exist(['ErrorLog_',RunSection,'.txt'], 'file')
  32. delete(['ErrorLog_',RunSection,'.txt']);
  33. end
  34. if exist(['Diary_',RunSection,'.txt'], 'file')
  35. delete(['Diary_',RunSection,'.txt']);
  36. end
  37. diary(['Diary_',RunSection,'.txt'])
  38. ErrLog = fopen(['ErrorLog_',RunSection,'.txt'], 'a');
  39. %==================================%
  40. % GENERATE HEADMODELS & LEADFIELD: %
  41. %==================================%
  42. for g = 1:length(name.GroupID)
  43. NumSubj = length(name.SubjID{g});
  44. if UseProgBar == 1
  45. ppm = ParforProgMon...
  46. (['HEADMODELS & LEADFIELDS: ',name.GroupID{g},'. '], NumSubj, 1, 700, 80);
  47. end
  48. parfor s = 1:length(name.SubjID{g})
  49. for c = 1:length(name.CondID)
  50. % Load input data (no cfg.inputfile options available):
  51. SegMRI = LoadFTmat(paths.SegMRI{g}{s}, 'HdmLeadfield');
  52. MEGdata = LoadFTmat(paths.MEGdata{g}{s,c}, 'HdmLeadfield');
  53. if isempty(SegMRI) || isempty(MEGdata)
  54. continue;
  55. end
  56. % Generate headmodel:
  57. cfgHdm = [];
  58. cfgHdm = FTcfg.Hdm;
  59. cfgHdm.grad = MEGdata.grad; % Only required for localspheres
  60. disp('Generating Headmodel:')
  61. Headmodel = ft_prepare_headmodel(cfgHdm, SegMRI)
  62. CheckSavePath(paths.Hdm{g}{s,c}, 'HdmLeadfield');
  63. ParforSaveHdm(paths.Hdm{g}{s,c}, Headmodel)
  64. SegMRI = []; % Free memory
  65. % Compute leadfield:
  66. % Note: Do not use cfg.inputfile here, sometimes grad info cannot be found.
  67. cfgLead = [];
  68. cfgLead = FTcfg.Lead;
  69. cfgLead.vol = Headmodel;
  70. Headmodel = []; % Free memory
  71. disp('Computing Leadfield:')
  72. Leadfield = ft_prepare_leadfield(cfgLead, MEGdata)
  73. CheckSavePath(paths.Lead{g}{s,c}, 'HdmLeadfield');
  74. ParforSaveLead(paths.Lead{g}{s,c}, Leadfield)
  75. Leadfield = []; % Free memory
  76. MEGdata = [];
  77. end % Cond
  78. if UseProgBar == 1 && mod(s, 1) == 0
  79. ppm.increment(); % move up progress bar
  80. end
  81. end % Subj
  82. if UseProgBar == 1
  83. ppm.delete();
  84. end
  85. end % Group
  86. %=========================%
  87. % CHECK FOR OUTPUT FILES: %
  88. %=========================%
  89. for g = 1:length(name.GroupID)
  90. for s = 1:length(name.SubjID{g})
  91. for c = 1:length(name.CondID)
  92. if ~exist(paths.Hdm{g}{s,c}, 'file')
  93. fprintf(ErrLog, ['ERROR: Output headmodel file missing:'...
  94. '\n %s \n\n'], paths.Hdm{g}{s});
  95. end
  96. if ~exist(paths.Lead{g}{s,c}, 'file')
  97. fprintf(ErrLog, ['ERROR: Output leadfield file missing:'...
  98. '\n %s \n\n'], paths.Lead{g}{s});
  99. end
  100. end % Cond
  101. end % Subj
  102. end % Group
  103. %=================%
  104. if exist([pwd,'/ErrorLog_HdmLeadfield.txt'], 'file')
  105. LogCheck = dir('ErrorLog_HdmLeadfield.txt');
  106. if LogCheck.bytes ~= 0 % File not empty
  107. open('ErrorLog_HdmLeadfield.txt');
  108. else
  109. delete('ErrorLog_HdmLeadfield.txt');
  110. end
  111. end
  112. fclose(ErrLog);
  113. diary off
  114. end
  115. function ParforSaveHdm(OutputPath, Headmodel)
  116. Headmodel
  117. save(OutputPath, 'Headmodel');
  118. end
  119. function ParforSaveLead(OutputPath, Leadfield)
  120. Leadfield
  121. save(OutputPath, 'Leadfield');
  122. end